package bhbt.controller.member.ti;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import bhbt.controller.core.BaseController;
import bhbt.model.Daili;
import bhbt.model.Member;
import bhbt.model.ParamWage;
import bhbt.model.log.LogMoneyTi;
import bhbt.service.DailiService;
import bhbt.service.MemberService;
import bhbt.service.ParamWageService;
import bhbt.service.log.LogMoneyTiService;
import bhbt.utils.ContextUtil;
import bhbt.utils.CryptUtil;
import bhbt.utils.MapUtil;





@Controller
 
public class TiController extends BaseController {

	@Resource
	private MemberService memberService;
	@Resource
	private LogMoneyTiService logMoneyTiService;
	@Resource
	private DailiService dailiService;
	
	@Resource
	private ParamWageService paramWageService;

	@RequestMapping("member/ti/tiShow.shtml")
	public String show(HttpServletRequest request) throws Exception{
		
		//会员信息
		Member member = ContextUtil.getMember();
		member= memberService.getByKey(member.getId());
		request.setAttribute("member",member);
		
		ParamWage paramWage1011=paramWageService.getByKey(1011);
		request.setAttribute("paramWage1011",paramWage1011.getValue());
		
		ParamWage paramWage1012=paramWageService.getByKey(1012);
		request.setAttribute("paramWage1012",paramWage1012.getValue());
		
		ParamWage paramWage1017=paramWageService.getByKey(1017);
		request.setAttribute("paramWage1017",paramWage1017.getValue());
		
		request.setAttribute("bankName",member.getBankName());
		request.setAttribute("bankPersonName",member.getBankPersonName());
		request.setAttribute("bankCardNo",member.getBankCardNo());
		request.setAttribute("bankSheng",member.getBankSheng());
		request.setAttribute("bankShi",member.getBankShi());
		request.setAttribute("bankAddress",member.getBankAddress());
		
		return "member/ti/ti";
	}
 
	@RequestMapping("member/ti/ti.shtml")
	public String ti(HttpServletRequest request, ModelMap model) throws Exception{
		
		Boolean result=true;
		//会员信息
		Member member = ContextUtil.getMember();
		member= memberService.getByKey(member.getId());
		Daili daili = this.dailiService.getByKey(member.getDailiId());
	 
		ParamWage paramWage1017=paramWageService.getByKey(1017);
		ParamWage paramWage1011 = this.paramWageService.getByKey(1011);
		ParamWage paramWage1012 = this.paramWageService.getByKey(1012);
		Map formMap = getParameterMap(request);
		String myTwoPassword = MapUtil.getString(formMap, "myTwoPassword");
		String pwd = CryptUtil.getInstance().encrypt2String(myTwoPassword,CryptUtil.keyStr);
   	 
    	if(!member.getTwoPassword().equals(pwd)){
    		setMessage(model,"二级密码不正确。");
    		result=false;
    	}else{
    		Double num = MapUtil.getDoubleValue(formMap, "num");
    		int type = MapUtil.getIntValue(formMap, "type");
    		if(num<=0){
    			setMessage(model,"提现金额填写不正确");
        		result=false;
    		}else{
    			
    			
				
				 
				
				if(num<paramWage1011.getValue()){
					setMessage(model,"提现金额必须大于："+paramWage1011.getValue());
					result=false;
				}
				
				
				if((num%paramWage1012.getValue())!=0){
					
					setMessage(model,"提现金额必须是"+paramWage1012.getValue()+"的整数！");
					result=false;
				}
				
				 
				
				Double fei = num*paramWage1017.getValue()*0.01;
				
				
				if(member.getMoney()<num){
    				setMessage(model,"现金币不足，提现金额："+num);
            		result=false;
    			}  
				
				
				
				if(result){
					LogMoneyTi logMoneyTi = new LogMoneyTi();
    				//订单编号
    				Random random = new Random(); 
    				SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss"); 
    				String tiCodeNo="T"+formatter.format(new Date()) + random.nextInt(2) ;
    				logMoneyTi.setTiCodeNo(tiCodeNo);
    				
    				logMoneyTi.setMemberId(member.getId());
    				logMoneyTi.setNum(num);
    				logMoneyTi.setFei(fei);
    				logMoneyTi.setShi(num-fei);
    				logMoneyTi.setType(type);
    				logMoneyTi.setBankPersonName(MapUtil.getString(formMap, "bankPersonName"));
    				logMoneyTi.setBankName(MapUtil.getString(formMap, "bankName"));
    				logMoneyTi.setBankCardNo(MapUtil.getString(formMap, "bankCardNo"));
    				logMoneyTi.setBankAddress(MapUtil.getString(formMap, "bankAddress"));
    				logMoneyTi.setBankSheng(MapUtil.getString(formMap, "bankSheng"));
    				logMoneyTi.setBankShi(MapUtil.getString(formMap, "bankShi"));
    				logMoneyTi.setBankPersonName(MapUtil.getString(formMap, "bankPersonName"));
    				logMoneyTi.setZhifuNo(MapUtil.getString(formMap, "zhifuCardNo"));
    				logMoneyTi.setZhifuPersonName(MapUtil.getString(formMap, "zhifuName"));
    				logMoneyTiService.saveTi(logMoneyTi);
    				return "redirect:moneyTiList.shtml";
				}
				
    		}
    		 
    	}
		
    	if(!result){
    		
    		request.setAttribute("member",member);
    		request.setAttribute("paramWage1011",paramWage1011.getValue());
    		request.setAttribute("paramWage1012",paramWage1012.getValue());
    		request.setAttribute("paramWage1017",paramWage1017.getValue());
    		return "member/ti/ti";
    	} 
		
		return "redirect:moneyTiList.shtml";
	}
	
	 
	
}
